﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：流程模板管理	
 */
var refreshGirdData; // 更新数据
var nowschemeId;
var selectedRow;
var userdefault = {};
var bootstrap = function ($, learun) {
    "use strict";

    var categoryId = '';
    var companyId = '';

    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                        //page.search();
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 左侧数据加载
            $('#lr_left_tree').lrtree({
                url: config.base_server() + 'dictDatas/tree?access_token=' + config.getToken().access_token,
                param: {code: 'FlowSort'},
                nodeClick: function (item) {
                    categoryId = item.id;
                    $('#titleinfo').text(item.text);
                    page.search();
                }
            });
            $('#lr_left_tree').lrtreeSet('checkFirst');
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                if (!companyId) {
                    learun.alert.warning('请选择机构！');
                    return false;
                }
                learun.layerForm({
                    id: 'Form',
                    title: '新增流程模板',
                    url: config.webportal_server + 'pages/workflow3/nwfScheme/form.html?companyId=' + companyId + '&categoryId=' + categoryId + '&access_token=' + config.getToken().access_token,
                    width: 1200,
                    height: 900,
                    maxmin: true,
                    btn: null
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var name = $('#gridtable').jfGridValue('name');
                var keyValue = $('#gridtable').jfGridValue('id');
                var code = $('#gridtable').jfGridValue('code');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'Form',
                        title: '编辑流程模板-' + name,
                        url: config.webportal_server + 'pages/workflow3/nwfScheme/form.html?companyId=' + companyId + '&keyValue=' + keyValue + '&categoryId=' + categoryId + '&shcemeCode=' + code + '&access_token=' + config.getToken().access_token,
                        width: 1200,
                        height: 900,
                        maxmin: true,
                        btn: null
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('是否确认删除该项！', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'nwfschemes/deleteForm?keyValue=' + keyValue + '&access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });

            // 预览模板
            $('#lr_preview').on('click', function () {
                var schemeId = $('#gridtable').jfGridValue('schemeId');
                if (learun.checkrow(schemeId)) {
                    learun.layerForm({
                        id: 'PreviewForm',
                        title: '预览当前模板',
                        url: config.webportal_server + 'pages/workflow3/nwfScheme/previewForm.html?schemeId=' + schemeId + '&access_token=' + config.getToken().access_token,
                        width: 1200,
                        height: 900,
                        maxmin: true,
                        btn: null
                    });
                }
            });

            // 启用
            $('#lr_enable').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                var enabledMark = $('#gridtable').jfGridValue('enabledMark');

                var type = $('#gridtable').jfGridValue('taskType');
                if (type == 2) {
                    learun.alert.warning('草稿模板不能启用!');
                    return false;
                }

                if (learun.checkrow(keyValue)) {
                    if (enabledMark != 1) {
                        learun.layerConfirm('是否确认启用该表单模板！', function (res) {
                            if (res) {
                                learun.postForm(config.base_server() + 'nwfschemes/updateSate?keyValue=' + keyValue +'&state=1&access_token=' + config.getToken().access_token, {
                                    keyValue: keyValue,
                                    state: 1
                                }, function () {
                                    refreshGirdData();
                                });
                            }
                        });
                    } else {
                        learun.alert.warning('该模板已启用!');
                    }
                }
            });
            // 禁用
            $('#lr_disabled').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                var enabledMark = $('#gridtable').jfGridValue('enabledMark');

                var type = $('#gridtable').jfGridValue('taskType');
                if (type == 2) {
                    learun.alert.warning('草稿模板不能禁用!');
                    return false;
                }

                if (learun.checkrow(keyValue)) {
                    if (enabledMark == 1) {
                        learun.layerConfirm('是否确认禁用该表单模板！', function (res) {
                            if (res) {
                                learun.postForm(config.base_server() + 'nwfschemes/updateSate?keyValue=' + keyValue +'&state=0&access_token=' + config.getToken().access_token, {
                                    keyValue: keyValue,
                                    state: 0
                                }, function () {
                                    refreshGirdData();
                                });

                            }
                        });
                    } else {
                        learun.alert.warning('该表单已禁用!');
                    }
                }
            });
            // 查看历史记录
            $('#lr_history').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                var Name = $('#gridtable').jfGridValue('name');
                nowschemeId = $('#gridtable').jfGridValue('schemeId');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'HistoryForm',
                        title: '流程模板历史记录-' + Name,
                        url: config.webportal_server + 'pages/workflow3/nwfScheme/historyForm.html?access_token=' + config.getToken().access_token + '&keyValue=' + keyValue,
                        width: 600,
                        height: 400,
                        maxmin: true,
                        btn: null
                    });
                }
            });
            // 导出
            $('#lr_export').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('F_Id');
                var code = $('#gridtable').jfGridValue('F_Code');
                if (learun.checkrow(keyValue)) {
                    learun.download({
                        method: "POST",
                        url: '/LR_NewWorkFlow/NWFScheme/ExportScheme',
                        param: {
                            code: code
                        }
                    });
                }
            });
            // 导入
            $('#lr_import').on('click', function () {
                learun.layerForm({
                    id: 'ImportForm',
                    title: '导入模板',
                    url: config.webportal_server + 'pages/workflow3/nwfScheme/importForm?access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    maxmin: true,
                    btn: null
                });
            });

            /*分类管理*/
            $('#lr_category').on('click', function () {
                learun.layerForm({
                    id: 'ClassifyIndex',
                    title: '分类管理',
                    url: config.webportal_server + 'pages/system/dict/detailIndex.html?access_token=' + config.getToken().access_token + '&itemCode=FlowSort',
                    width: 800,
                    height: 500,
                    maxmin: true,
                    btn: null,
                    end: function () {
                        learun.clientdata.update('dataItem');
                        // location.reload();
                    }
                });
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'nwfschemes/getInfoPageList?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "编号", name: "code", width: 150, align: "left"},
                    {label: "名称", name: "name", width: 160, align: "left"},
                    {
                        label: "分类", name: "category", width: 120, align: "center",
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'FlowSort',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    },
                    {
                        label: "状态", name: "enabledMark", width: 50, align: "center",
                        formatter: function (cellvalue, row) {
                            if (row.type == 1) {
                                if (cellvalue == 1) {
                                    return '<span class=\"label label-success\" style=\"cursor: pointer;\">正常</span>';
                                } else if (cellvalue == 0) {
                                    return '<span class=\"label label-default\" style=\"cursor: pointer;\">禁用</span>';
                                }
                            } else {
                                return '<span class=\"label label-info\" style=\"cursor: pointer;\">草稿</span>';
                            }
                        }
                    },
                    {label: "编辑人", name: "createUsername", width: 80, align: "center"},
                    {
                        label: "编辑时间", name: "createTime", width: 150, align: "center",
                        formatter: function (cellvalue) {
                            return learun.formatDate(cellvalue, 'yyyy-MM-dd hh:mm:ss');
                        },
			            sort: true
                    },
                    {label: "备注", name: "description", width: 200, align: "left"}
                ],
                dblclick: function (row) {
                    var schemeId = row.schemeId;
                    if (learun.checkrow(schemeId)) {
                        learun.layerForm({
                            id: 'PreviewForm',
                            title: '预览当前模板',
                            url: config.webportal_server + 'pages/workflow3/nwfScheme/previewForm.html?schemeId=' + schemeId + '&access_token=' + config.getToken().access_token,
                            width: 1200,
                            height: 900,
                            maxmin: true,
                            btn: null
                        });
                    }
                },
                mainId: 'id',
                reloadSelected: true,
                isPage: true
            });
        },
        search: function (param) {
            param = param || {};
            param.category = categoryId;
            param.companyId = companyId;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    }

    page.init();
}


